package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.CourseDto;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        courseService.delete(id);
        return Result.success();
    }

    @GetMapping
    public Result seleteall(CourseDto coursedto){
        PageResult<Course> pageResult = courseService.selectall(coursedto);
        return Result.success(pageResult);
    }

    @Log
    @PostMapping
    public Result add(@RequestBody Course course){
        courseService.add(course);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Course course = courseService.getById(id);
        return Result.success(course);
    }

    @Log
    @PutMapping
    public Result update(@RequestBody Course course){
        courseService.update(course);
        return Result.success();
    }

    @GetMapping("/list")
    public Result all(){
        List<Course> list = courseService.all();
        return Result.success(list);
    }

    @GetMapping("/subject/{subject}")
    public Result getBySubject(@PathVariable Integer subject){
        List<Course> list = courseService.getBysubject(subject);
        return Result.success(list);
    }
}
